#include <iostream>
#include <sstream>
#include <iomanip>

#include "db_common.hpp"

using namespace std;

static const std::string ROLE_DB="role_";

std::string DBCommon::role_table(RID rid)
{
	RID index = (rid % ROLE_TABLE_NUM);

	std::ostringstream oss;
	oss << ROLE_DB << hex << setw(2) << setfill('0')
		<< setiosflags(ios::uppercase) << index;

	return oss.str();
}

const std::string DBCommon::op_type_to_str(int type)
{
	const static std::string OP_TYPE[] = {"DB_OP_NONE", "DB_OP_INSERT", \
						"DB_OP_DELETE", "DB_OP_UPDATE", "DB_OP_SELECT", "DB_OP_COUNT"};

	return ((type >= DB_OP_MAX)? "DB_OP_UNKNOWN": OP_TYPE[type]);
}

const std::string DBCommon::field_type_to_str(int type)
{
	const static std::string FIELD_TYPE[] = {"DB_FIELD_UNKNOWN", "DB_FIELD_INT8", 
					"DB_FIELD_INT16", "DB_FIELD_INT32", "DB_FIELD_INT64", 
					"DB_FIELD_STRING", "DB_FIELD_BLOB"};

	return ((type >= DB_FIELD_MAX)? "DB_FIELD_UNKNOWN": FIELD_TYPE[type]);
}

const std::string DBCommon::fetch_err_to_str(int err_code)
{
	const static std::string FETCH_ERR[] = {"E_DB_TYPE_SUCC", "E_DB_TYPE_SMALL", "E_DB_TYPE_MATCH"};

	return ((err_code >= E_DB_FETCH_MAX)? "E_DB_UNKNOWN": FETCH_ERR[err_code]);
}

